package com.tencent.examples.blueking.mongo.service;

import com.mongodb.BasicDBObject;
import com.tencent.examples.blueking.mongo.extend.biz.CrudBiz;
import com.tencent.examples.blueking.extend.QueryParam;
import com.tencent.examples.blueking.mongo.model.ObjClassification;
import com.tencent.examples.blueking.mongo.repository.ObjClassificationRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class ObjClassificationService extends CrudBiz<ObjClassificationRepository> {

    @Resource
    private MongoTemplate mongoTemplate;

    public void test() {
        QueryParam queryParam = new QueryParam();
        Pageable pageable = new PageRequest(1, 200, Sort.Direction.DESC, "_id");
        List<BasicDBObject> list = getAggregation(queryParam, pageable);
        System.out.println("11");
    }

    public List<ObjClassification> findFirstMenu(){
        List<ObjClassification> objClassificationList=dao.findFirstMenu();
        return objClassificationList;
    }

    /**
     * 原生多表查询（一对多）
     *
     * @param queryParam
     * @return
     */
    public List<BasicDBObject> getAggregation(QueryParam queryParam, Pageable pageable) {
        Map<String, String> dataMap = queryParam.getSqlMap();
        Criteria criteria = new Criteria();
        String bk_classification_name = dataMap.get("bk_classification_name");
        if (StringUtils.isNotEmpty(bk_classification_name)) {
            criteria.and("bk_classification_name").is(bk_classification_name);
        }
        String bk_obj_name = dataMap.get("bk_obj_name");
        if (StringUtils.isNotEmpty(bk_obj_name)) {
            criteria.and("objDes.bk_obj_name").is(bk_obj_name);
        }
        Aggregation aggregation = Aggregation.newAggregation(
                Aggregation.lookup("cc_ObjDes", "bk_classification_id", "bk_classification_id", "objDes"),//lookupAgg一定要在matchOperation的前面 两者顺序颠倒就过滤不了
                Aggregation.match(criteria),//查询条件
//                project("cc_ObjClassification.*", "objDes.bk_obj_name"),//输出字段，默认全部
//                Aggregation.group("bk_classification_id").count().as("count1"),
//                Aggregation.count().as("count"),project("count"),//查询总数
                Aggregation.sort(pageable.getSort()),//排序字段
                Aggregation.skip(pageable.getPageNumber() > 1 ? (pageable.getPageNumber() - 1) * pageable.getPageSize() : 0),//分页操作，pageNumber为当前页数，pageSize为每页显示条数
                Aggregation.limit(pageable.getPageSize()));//页数
        AggregationResults<BasicDBObject> ret = mongoTemplate.aggregate(aggregation, "cc_ObjClassification", BasicDBObject.class);
        return ret.getMappedResults();
    }


}
